import java.util.ArrayList;
import java.util.List;

/**
 * @author yexy
 * @date 2020/6/15 9:18 上午
 * @description
 */
public class StudentManage {

    private ArrayList<Student> studentList;

    public StudentManage() {
       if (this.studentList == null)
       {
           //初始化数据
           this.studentList = new ArrayList<>();
           studentList.add(new Student("1","tom",11));
           studentList.add(new Student("2","sam",11));
           studentList.add(new Student("3","amy",11));
           studentList.add(new Student("4","amy",14));
       }

    }

    //添加学生
    public void add(Student s){
        this.studentList.add(s);
    }

    //根据学号删除
    public void deleteByNo(String no){
        Student oldStu = getByNo(no);
        this.studentList.remove(oldStu);
    }

    //根据学号查询
    public Student getByNo(String no){
        Student getS=null;
        for (Student student : studentList) {
            if (student.getNo().equals(no))
            {
                getS = student;
                break;
            }

        }
        return getS;
    }

    //根据姓名查询
    public List<Student> queryByName(String name){
        ArrayList<Student> getS=new ArrayList<>();
        for (Student student : studentList) {
            if (student.getName().equals(name))
                getS.add(student);
        }
        return getS;
    }

    //根据年龄查询
    public List<Student> queryByAge(Integer age){
        ArrayList<Student> getS=new ArrayList<>();
        for (Student student : studentList) {
            if (student.getAge().equals(age))
                getS.add(student);
        }
        return getS;
    }

    //根据学号修改
    public void modifyByNo(String no,Student s){
         Student oldStu = getByNo(no);
         oldStu.setNo(s.getNo());
         oldStu.setName(s.getName());
         oldStu.setAge(s.getAge());
    }

    //show方法
    public String show(){
        String result = "";
        for (Student student : studentList) {
            result += student.toString() + "\n";
        }
        return result;
    }

    //show方法
    public String show(List<Student> list){
        String result = "";
        for (Student student : list) {
            result += student.toString() + "\n";
        }
        return result;
    }

}
